package com.cszy.yydqbfq.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.hbcysd.qybfq.R
import com.hbcysd.qybfq.databinding.ActivityYanZhengPwdBinding

import com.cszy.yydqbfq.base.BaseVMActivity
import com.cszy.yydqbfq.common.AppConstants

import com.cszy.yydqbfq.ext.launch
import com.cszy.yydqbfq.ext.toast
import com.cszy.yydqbfq.service.AntiAddictionService
import com.cszy.yydqbfq.utils.SpUtils
import com.cszy.yydqbfq.viewmodel.MainViewModel


class ActivityYanZhengPwdActivity : BaseVMActivity<MainViewModel, ActivityYanZhengPwdBinding>() {
    private var codeSrt:String?=null
    override fun initView(savedInstanceState: Bundle?) {

    }

    override fun initData() {
       codeSrt = intent.getStringExtra(AppConstants.SETTING_PWD)
    }

    override fun initListener() {
        mBinding.headTitle.headCenterTitle.text = "验证密码"
        mBinding.headTitle.headCenterTitle.setTextColor(resources.getColor(R.color.black))
        mBinding.headTitle.headBack.setOnClickListener {
            finish()
        }
        setupAutoFocus()
        //点击下一步
        mBinding.btnNext.setOnClickListener {
            if (TextUtils.isEmpty(mBinding.edit1.text)|| TextUtils.isEmpty(mBinding.edit2.text)||
                TextUtils.isEmpty(mBinding.edit3.text)|| TextUtils.isEmpty(mBinding.edit4.text)
            ) {
                toast("请输入完整四位数密码")
                return@setOnClickListener
            }
            val last4Digits = getPhoneLast4Digits()
            if (last4Digits==codeSrt){
                SpUtils.getInstance().putString(AppConstants.SETTING_PWD,last4Digits)
                SpUtils.getInstance().putBoolean(AppConstants.IS_SHOW_FCM_DIALOG,true)
                SpUtils.getInstance().putBoolean(AppConstants.ISOPEN_FCM,true)
                toast("开启青少年模式成功")
//                val serviceIntent = Intent(this, AntiAddictionService::class.java).apply {
//                    putExtra(if (true) "start" else "stop", true)
//                }
//                startService(serviceIntent)
                finish()
            }else{
                toast("两次输入的密码不一致，请返回重新设置")

            }

        }
    }

    /**
     * 自动跳转输入
     */
    private fun setupAutoFocus() {
        val editTexts = arrayOf(mBinding.edit1, mBinding.edit2, mBinding.edit3, mBinding.edit4)
        editTexts.forEachIndexed { index, editText ->
            editText.addTextChangedListener(object : TextWatcher {
                override fun afterTextChanged(s: Editable?) {
                    if (s?.length == 1 && index < editTexts.lastIndex) {
                        editTexts[index + 1].requestFocus()
                    } else if (s?.length == 1 && index == editTexts.lastIndex) {
                        hideKeyboard()
                    }
                }
                override fun beforeTextChanged(
                    s: CharSequence?,
                    start: Int,
                    count: Int,
                    after: Int
                ) {
                }

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
            })
        }
    }

    // 隐藏键盘
    private fun hideKeyboard() {
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(mBinding.edit4.windowToken, 0)
    }
    fun getPhoneLast4Digits(): String {
        return "${mBinding.edit1.text}${mBinding.edit2.text}${mBinding.edit3.text}${mBinding.edit4.text}"
    }

}